﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class test_memcache_demo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string cachKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HOUSE, "11111");
        Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HOUSE);
        System.Data.DataSet ds = cs.GetDataSet(cachKey);
        if (!cs.Hit)
        {
            this.Response.Write("errr");
            ds = GetNewDataSet();
            cs.Set(cachKey, ds);
        }
        this.GridView1.DataSource = ds.Tables[0];
        this.GridView1.DataBind();
        this.Response.Write("OK");
    }

    private System.Data.DataSet GetNewDataSet()
    {
        System.Data.DataSet ds = new DataSet();
        System.Data.DataTable dt = new DataTable();
        dt.Columns.Add("id", typeof(int));
        dt.Columns.Add("code", typeof(string));
        dt.Columns.Add("name", typeof(string));

        for (int i = 1; i < 100; i++)
        {
            System.Data.DataRow row = dt.NewRow();
            row["id"] = i;
            row["code"] = "code:" + i.ToString();
            row["name"] = "name:" + i.ToString();

            dt.Rows.Add(row);
        }


        ds.Tables.Add(dt);

        return ds;
    }

    public static string GetSpell(string input)
    {
        int len = input.Length;
        string reVal = "";
        for (int i = 0; i < len; i++)
        {
            reVal += getSpell(input.Substring(i, 1));
        }
        return reVal;
    }
    public static string getSpell(string cn)
    {
        byte[] arrCN = Encoding.Default.GetBytes(cn);
        if (arrCN.Length > 1)
        {
            int area = (short)arrCN[0];
            int pos = (short)arrCN[1];
            int code = (area << 8) + pos;
            int[] areacode = new int[] { 
					0xb0a1, 0xb0c5, 0xb2c1, 0xb4ee, 0xb6ea, 0xb7a2, 0xb8c1, 0xb9fe, 0xbbf7, 0xbbf7, 
					0xbfa6, 0xc0ac, 0xc2e8, 0xc4c3, 0xc5b6, 0xc5be, 
					0xc6da, 0xc8bb, 0xc8f6, 0xcbfa, 0xcdda, 0xcdda, 0xcdda, 0xcef4, 0xd1b9, 0xd4d1
				};
            for (int i = 0; i < 26; i++)
            {
                int max = 55290;
                if (i != 25) max = areacode[i + 1];
                if (areacode[i] <= code && code < max)
                {
                    return Encoding.Default.GetString(new byte[] { (byte)(65 + i) });
                }
            }
            return "?";
        }
        else return cn;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string a = this.TextBox1.Text.Trim();
        this.TextBox1.Text = GetSpell(a);
    }
}
